package com.example.idea.service;

import com.example.idea.entity.Notice;
import com.example.idea.mapper.NoticeMapper;
import com.example.idea.utils.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class NoticeService {

    @Resource
    private NoticeMapper noticeMapper;

    public void add(Notice notice) {
        notice.setTime(new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date()));
        notice.setOpen(false);
        notice.setUserid(1);
        noticeMapper.add(notice);
    }

    public void update(Notice notice) {
        noticeMapper.update(notice);
    }

    public void delete(Integer id) {
        noticeMapper.delete(id);
    }

    public Notice query(Integer id) {
        return noticeMapper.query(id);
    }

    public void deleteBatch(List<Integer> ids) {
        String idsStr = ids.toString().replace("[", "").replace("]", "");
        noticeMapper.deleteBatch(idsStr);
    }

    public List<Notice> list() {
        return noticeMapper.list();
    }

    public List<Notice> selectList(Notice notice) {
        return noticeMapper.selectList(notice);
    }

    public Page<Notice> selectByPage(Integer pageNum, Integer pageSize, String title, String time) {
        Integer skipNum = (pageNum - 1) * pageSize;
        Page<Notice> page = new Page<>();
        List<Notice> newsList = noticeMapper.selectByPage(skipNum, pageSize, title, time);
        Integer total = noticeMapper.selectCountByPage(title, time);
        page.setList(newsList);
        page.setTotal(total);
        return page;
    }
}
